# -*- coding: utf-8 -*-
"""
Created on Fri Dec 25 22:48:04 2015

@author: liuyi05
"""

import tushare as ts
import pandas as pd

def fetch_basic_infos(file_path):
    df = ts.get_stock_basics()
    df = df[df["timeToMarket"] != 0]
    date = df["timeToMarket"]
    df["timeToMarket"] = date.apply(lambda x: str(x)).apply(lambda x: 
            "%s-%s-%s" % (x[:4], x[4:6], x[6:8]))
    df.to_csv(file_path)
    return df
    
def load_basic_infos(file_path):
    return pd.read_csv(file_path, encoding="gbk")

def main():
    file_path = "data/stock_basic_list.csv"
    df = fetch_basic_infos(file_path)
    #df = load_basic_infos(file_path)
    code_list = df["code"].copy()
    code_list.sort_values(inplace=True)
    
    code_file = open("data/code.list", "w")
    for code in code_list:
        print("%06d" % code, file=code_file)
    code_file.close()
    
if __name__ == "__main__":
    main()